package chapter07

import scala.collection.mutable.ArrayBuffer

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/10 9:21 AM
 */
object Test02_ArrayBuffer {
  def main(args: Array[String]): Unit = {
    // 1.创建可变数组
    val arr1 = new ArrayBuffer[Int]()
    val arr2 = ArrayBuffer(23, 57, 92)

    println(arr1.mkString(","))
    println(arr2.toString())

    println(arr2(2))
    arr2(1) = 39
    println(arr2(1))
    // 3.添加元素
    val newArr1 = arr1 :+ 15
    println(newArr1)

    arr1 += 19
    println(arr1)
    val newArr2 = arr1 += 19
    println(arr1 == newArr2)

    15 +=: arr1
    println(arr1)

    arr1.append(36, 14)
    arr1.prepend(12, 13)
    arr1.insert(3, 15, 16, 17)
    println(arr1)

    arr1.insertAll(1, arr2)
    arr1.prependAll(arr1)
    println(arr1)

    println("=" * 20)
    // 4.删除元素
    arr1.remove(3)
    arr1.remove(2, 10)
    println(arr1)
    arr1 -= 15
    println(arr1)

    println("=" * 20)
    // 5.可变数组转换为不可变数组
    val arr = ArrayBuffer(13, 34, 56)
    val newArr = arr.toArray
    println(newArr.mkString(","))

    // 6.不可变数组转换为可变数组
    val buffer = newArr.toBuffer
    println(buffer)
    println(newArr)
  }
}
